Flutter / Basic / New Project
New Project
-
Steps
Step 1:
Open Android StudioStep 2:
Select Flutter New Project optionStep 3:
Clear lib/main.dart and add followingsUsing Simple code
import 'package:flutter/material.dart'; // the main() function is the starting point for every Flutter project void main() { // calling this method (you guessed it) runs our app runApp( Container( color: Colors.green, // <-- change this ), ); } Using class
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return const MaterialApp( home: Text("Hello World Niluka"), ); } } App bar & body
We must use 'Scaffold layout' for creating page tree import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Welcome to Flutter', home: Scaffold( appBar: AppBar( title: const Text('Welcome to Flutter'), ), body: const Center( child: Text('Hello World'), ), ), ); } } 1) appBar inside home
2) body inside homeExamples
Change home property of 'MaterialApp'
Container
Columnhome: Scaffold( body: Container( color: Colors.blue, child: Center( child: Text('Hello, Flutter!'), ), ), )
RowScaffold( body: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Item 1'), Text('Item 2'), Text('Item 3'), ], ), )
ListViewScaffold( body: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Item 1'), Text('Item 2'), Text('Item 3'), ], ), )
ExpandedScaffold( body: ListView( children: [ ListTile(title: Text('Item 1')), ListTile(title: Text('Item 2')), ListTile(title: Text('Item 3')), ], ), )
StackScaffold( body: Column( children: [ Text('Fixed Height Widget'), Expanded( child: Container( color: Colors.blue, ), ), ], ), ) Scaffold( body: Stack( children: [ Positioned( top: 50, left: 50, child: Text('Overlay Text'), ), Center( child: Image.asset('assets/image.png'), ), ], ), )